package entities;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import utils.ChocoDate;

/**
 * The Action classe contain the information about one action that can be a deficit or adding to the count amount.
 * @author chocoseb
 */
public class Action {
    private Calendar _date;
    private double _amount;
    private String _description;
    private boolean _confirmed;
    private Month _month;
    private Categorie _categorie;

    public Action(){
        _date = ChocoDate.now();
        _amount = 0.00;
        _description = "";
        _confirmed = true;
        _month = null;
        _categorie = null;
    }

    /**
     * Create a new action from scratch
     * @param p_month The month in wich the action is being done.
     */
    public Action(Month p_month){
        _date = ChocoDate.now();
        _amount = 9.36;
        _description = "Diner chez oeufs";
        _confirmed = true;
        _month = p_month;
        _month.add(this);
    }

    /**
     * Create a new action in the program base on the file containing all the months actions.
     * @param p_month The month in wich the action is being done.
     * @param in The DataInputStream that represent an open file for a month of type .bdj
     * @throws IOException If the file is corrupted.
     */
    public Action (Month p_month, DataInputStream in) throws IOException{
        _date = new GregorianCalendar();
        _date.setTimeInMillis(in.readLong());
        _amount = in.readDouble();
        _description = in.readUTF();
        _confirmed = in.readBoolean();
        String temp = in.readUTF();
        if (!temp.equals("Autre")){
            _categorie =  Categorie.findByName(temp, Categorie.getCategorieList());
        }
        _month = p_month;
    }

    public double getAmount() {
        return _amount;
    }

    public void setAmount(double _amount) {
        this._amount = _amount;
    }

    public Calendar getDate() {
        return _date;
    }

    /**
     * Set the Date with the new Calendar object
     * @param _date
     */
    public void setDate(Calendar _date) {
        this._date = _date;
    }

    /**
     * Set the date with an old Date object
     * @param _date
     */
    public void setDate(Date _date) {
        Calendar newDate = new GregorianCalendar();
        newDate.setTime(_date);
        this._date = newDate;
    }

    public String getDescription() {
        return _description;
    }

    public void setDescription(String _description) {
        this._description = _description;
    }

    public Categorie getCategorie() {
        return _categorie;
    }

    public void setCategorie(Categorie _categorie) {
        this._categorie = _categorie;
    }

    public Month getMonth() {
        return _month;
    }

    public void setMonth(Month _month) {
        this._month = _month;
    }

    

    /**
     * Save the action into an open file for the month.
     * @param out The DataOuptutStream in wich the action will be saved
     * @throws IOException If the file is corrupted
     */
    public void save(DataOutputStream out) throws IOException{
        out.writeLong(_date.getTimeInMillis());
        out.writeDouble(_amount);
        out.writeUTF(_description);
        out.writeBoolean(_confirmed);
        if (_categorie == null){
            out.writeUTF("Autre");
        }else{
            out.writeUTF(_categorie.getName());
        }

    }

}
